home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue3 / Games / xrick / !xrick / src / c / unzip < prev    next >
Text File  |  2004-06-24  |  35KB  |  1,321 lines

  1. /* unzip.c -- IO on .zip files using zlib
  2.    Version 0.15 beta, Mar 19th, 1998,
  3.  
  4.    Read unzip.h for more info
  5. */
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "zlib.h"
  12. #include "unzip.h"
  13.  
  14. #ifdef STDC
  15. #  include <stddef.h>
  16. #  include <string.h>
  17. #  include <stdlib.h>
  18. #endif
  19. #ifdef NO_ERRNO_H
  20.     extern int errno;
  21. #else
  22. #   include <errno.h>
  23. #endif
  24.  
  25.  
  26. #ifndef local
  27. #  define local static
  28. #endif
  29. /* compile with -Dlocal if your debugger can't find static symbols */
  30.  
  31.  
  32.  
  33. #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
  34.                       !defined(CASESENSITIVITYDEFAULT_NO)
  35. #define CASESENSITIVITYDEFAULT_NO
  36. #endif
  37.  
  38.  
  39. #ifndef UNZ_BUFSIZE
  40. #define UNZ_BUFSIZE (16384)
  41. #endif
  42.  
  43. #ifndef UNZ_MAXFILENAMEINZIP
  44. #define UNZ_MAXFILENAMEINZIP (256)
  45. #endif
  46.  
  47. #ifndef ALLOC
  48. # define ALLOC(size) (malloc(size))
  49. #endif
  50. #ifndef TRYFREE
  51. # define TRYFREE(p) {if (p) free(p);}
  52. #endif
  53.  
  54. #define SIZECENTRALDIRITEM (0x2e)
  55. #define SIZEZIPLOCALHEADER (0x1e)
  56.  
  57.  
  58. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  59.  
  60. #ifndef SEEK_CUR
  61. #define SEEK_CUR    1
  62. #endif
  63.  
  64. #ifndef SEEK_END
  65. #define SEEK_END    2
  66. #endif
  67.  
  68. #ifndef SEEK_SET
  69. #define SEEK_SET    0
  70. #endif
  71.  
  72. const char unz_copyright[] =
  73.    " unzip 0.15 Copyright 1998 Gilles Vollant ";
  74.  
  75. /* unz_file_info_interntal contain internal info about a file in zipfile*/
  76. typedef struct unz_file_info_internal_s
  77. {
  78.     uLong offset_curfile;/* relative offset of local header 4 bytes */
  79. } unz_file_info_internal;
  80.  
  81.  
  82. /* file_in_zip_read_info_s contain internal information about a file in zipfile,
  83.     when reading and decompress it */
  84. typedef struct
  85. {
  86.     char  *read_buffer;         /* internal buffer for compressed data */
  87.     z_stream stream;            /* zLib stream structure for inflate */
  88.  
  89.     uLong pos_in_zipfile;       /* position in byte on the zipfile, for fseek*/
  90.     uLong stream_initialised;   /* flag set if stream structure is initialised*/
  91.  
  92.     uLong offset_local_extrafield;/* offset of the local extra field */
  93.     uInt  size_local_extrafield;/* size of the local extra field */
  94.     uLong pos_local_extrafield;   /* position in the local extra field in read*/
  95.  
  96.     uLong crc32;                /* crc32 of all data uncompressed */
  97.     uLong crc32_wait;           /* crc32 we must obtain after decompress all */
  98.     uLong rest_read_compressed; /* number of byte to be decompressed */
  99.     uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
  100.     FILE* file;                 /* io structore of the zipfile */
  101.     uLong compression_method;   /* compression method (0==store) */
  102.     uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  103. } file_in_zip_read_info_s;
  104.  
  105.  
  106. /* unz_s contain internal information about the zipfile
  107. */
  108. typedef struct
  109. {
  110.     FILE* file;                 /* io structore of the zipfile */
  111.     unz_global_info gi;       /* public global information */
  112.     uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  113.     uLong num_file;             /* number of the current file in the zipfile*/
  114.     uLong pos_in_central_dir;   /* pos of the current file in the central dir*/
  115.     uLong current_file_ok;      /* flag about the usability of the current file*/
  116.     uLong central_pos;          /* position of the beginning of the central dir*/
  117.  
  118.     uLong size_central_dir;     /* size of the central directory  */
  119.     uLong offset_central_dir;   /* offset of start of central directory with
  120.                                    respect to the starting disk number */
  121.  
  122.     unz_file_info cur_file_info; /* public info about the current file in zip*/
  123.     unz_file_info_internal cur_file_info_internal; /* private info about it*/
  124.     file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
  125.                                         file if we are decompressing it */
  126.     char *filename;
  127. } unz_s;
  128.  
  129.  
  130. extern unzFile ZEXPORT unzDup(unzFile file)
  131. {
  132.     unz_s *x, *y;
  133.     unz_s t;
  134.  
  135.     /* cast */
  136.     x=(unz_s*)file;
  137.  
  138.     /* copy */
  139.     t = *x;
  140.  
  141.     /* tweek */
  142.     t.file = fopen(x->filename, "rb");
  143.     t.pfile_in_zip_read = NULL;
  144.  
  145.     /* cast & return */
  146.     y=(unz_s*)ALLOC(sizeof(unz_s));
  147.     *y=t;
  148.     unzGoToFirstFile((unzFile)y);
  149.     return (unzFile)y;
  150. }
  151.  
  152.  
  153. /* ===========================================================================
  154.      Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  155.    for end of file.
  156.    IN assertion: the stream s has been sucessfully opened for reading.
  157. */
  158.  
  159.  
  160. local int unzlocal_getByte(fin,pi)
  161.     FILE *fin;
  162.     int *pi;
  163. {
  164.     unsigned char c;
  165.     int err = fread(&c, 1, 1, fin);
  166.     if (err==1)
  167.     {
  168.         *pi = (int)c;
  169.         return UNZ_OK;
  170.     }
  171.     else
  172.     {
  173.         if (ferror(fin))
  174.             return UNZ_ERRNO;
  175.         else
  176.             return UNZ_EOF;
  177.     }
  178. }
  179.  
  180.  
  181. /* ===========================================================================
  182.    Reads a long in LSB order from the given gz_stream. Sets
  183. */
  184. local int unzlocal_getShort (fin,pX)
  185.     FILE* fin;
  186.     uLong *pX;
  187. {
  188.     uLong x ;
  189.     int i;
  190.     int err;
  191.  
  192.     err = unzlocal_getByte(fin,&i);
  193.     x = (uLong)i;
  194.  
  195.     if (err==UNZ_OK)
  196.         err = unzlocal_getByte(fin,&i);
  197.     x += ((uLong)i)<<8;
  198.  
  199.     if (err==UNZ_OK)
  200.         *pX = x;
  201.     else
  202.         *pX = 0;
  203.     return err;
  204. }
  205.  
  206. local int unzlocal_getLong (fin,pX)
  207.     FILE* fin;
  208.     uLong *pX;
  209. {
  210.     uLong x ;
  211.     int i;
  212.     int err;
  213.  
  214.     err = unzlocal_getByte(fin,&i);
  215.     x = (uLong)i;
  216.  
  217.     if (err==UNZ_OK)
  218.         err = unzlocal_getByte(fin,&i);
  219.     x += ((uLong)i)<<8;
  220.  
  221.     if (err==UNZ_OK)
  222.         err = unzlocal_getByte(fin,&i);
  223.     x += ((uLong)i)<<16;
  224.  
  225.     if (err==UNZ_OK)
  226.         err = unzlocal_getByte(fin,&i);
  227.     x += ((uLong)i)<<24;
  228.  
  229.     if (err==UNZ_OK)
  230.         *pX = x;
  231.     else
  232.         *pX = 0;
  233.     return err;
  234. }
  235.  
  236.  
  237. /* My own strcmpi / strcasecmp */
  238. local int strcmpcasenosensitive_internal (fileName1,fileName2)
  239.     const char* fileName1;
  240.     const char* fileName2;
  241. {
  242.     for (;;)
  243.     {
  244.         char c1=*(fileName1++);
  245.         char c2=*(fileName2++);
  246.         if ((c1>='a') && (c1<='z'))
  247.             c1 -= 0x20;
  248.         if ((c2>='a') && (c2<='z'))
  249.             c2 -= 0x20;
  250.         if (c1=='\0')
  251.             return ((c2=='\0') ? 0 : -1);
  252.         if (c2=='\0')
  253.             return 1;
  254.         if (c1<c2)
  255.             return -1;
  256.         if (c1>c2)
  257.             return 1;
  258.     }
  259. }
  260.  
  261.  
  262. #ifdef  CASESENSITIVITYDEFAULT_NO
  263. #define CASESENSITIVITYDEFAULTVALUE 2
  264. #else
  265. #define CASESENSITIVITYDEFAULTVALUE 1
  266. #endif
  267.  
  268. #ifndef STRCMPCASENOSENTIVEFUNCTION
  269. #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
  270. #endif
  271.  
  272. /*
  273.    Compare two filename (fileName1,fileName2).
  274.    If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
  275.    If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
  276.                                                                 or strcasecmp)
  277.    If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
  278.         (like 1 on Unix, 2 on Windows)
  279.  
  280. */
  281. extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
  282.     const char* fileName1;
  283.     const char* fileName2;
  284.     int iCaseSensitivity;
  285. {
  286.     if (iCaseSensitivity==0)
  287.         iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
  288.  
  289.     if (iCaseSensitivity==1)
  290.         return strcmp(fileName1,fileName2);
  291.  
  292.     return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
  293. }
  294.  
  295. #define BUFREADCOMMENT (0x400)
  296.  
  297. /*
  298.   Locate the Central directory of a zipfile (at the end, just before
  299.     the global comment)
  300. */
  301. local uLong unzlocal_SearchCentralDir(fin)
  302.     FILE *fin;
  303. {
  304.     unsigned char* buf;
  305.     uLong uSizeFile;
  306.     uLong uBackRead;
  307.     uLong uMaxBack=0xffff; /* maximum size of global comment */
  308.     uLong uPosFound=0;
  309.  
  310.     if (fseek(fin,0,SEEK_END) != 0)
  311.         return 0;
  312.  
  313.  
  314.     uSizeFile = ftell( fin );
  315.  
  316.     if (uMaxBack>uSizeFile)
  317.         uMaxBack = uSizeFile;
  318.  
  319.     buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  320.     if (buf==NULL)
  321.         return 0;
  322.  
  323.     uBackRead = 4;
  324.     while (uBackRead<uMaxBack)
  325.     {
  326.         uLong uReadSize,uReadPos ;
  327.         int i;
  328.         if (uBackRead+BUFREADCOMMENT>uMaxBack)
  329.             uBackRead = uMaxBack;
  330.         else
  331.             uBackRead+=BUFREADCOMMENT;
  332.         uReadPos = uSizeFile-uBackRead ;
  333.  
  334.         uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  335.                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
  336.         if (fseek(fin,uReadPos,SEEK_SET)!=0)
  337.             break;
  338.  
  339.         if (fread(buf,(uInt)uReadSize,1,fin)!=1)
  340.             break;
  341.  
  342.                 for (i=(int)uReadSize-3; (i--)>0;)
  343.             if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
  344.                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  345.             {
  346.                 uPosFound = uReadPos+i;
  347.                 break;
  348.             }
  349.  
  350.         if (uPosFound!=0)
  351.             break;
  352.     }
  353.     TRYFREE(buf);
  354.     return uPosFound;
  355. }
  356.  
  357. /*
  358.   Open a Zip file. path contain the full pathname (by example,
  359.      on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
  360.      "zlib/zlib109.zip".
  361.      If the zipfile cannot be opened (file don't exist or in not valid), the
  362.        return value is NULL.
  363.      Else, the return value is a unzFile Handle, usable with other function
  364.        of this unzip package.
  365. */
  366. extern unzFile ZEXPORT unzOpen (path)
  367.     const char *path;
  368. {
  369.     unz_s us;
  370.     unz_s *s;
  371.     uLong central_pos,uL;
  372.     FILE * fin ;
  373.  
  374.     uLong number_disk;          /* number of the current dist, used for
  375.                                    spaning ZIP, unsupported, always 0*/
  376.     uLong number_disk_with_CD;  /* number the the disk with central dir, used
  377.                                    for spaning ZIP, unsupported, always 0*/
  378.     uLong number_entry_CD;      /* total number of entries in
  379.                                    the central dir
  380.                                    (same than number_entry on nospan) */
  381.  
  382.     int err=UNZ_OK;
  383.  
  384.     if (unz_copyright[0]!=' ')
  385.         return NULL;
  386.  
  387.     fin=fopen(path,"rb");
  388.     if (fin==NULL)
  389.         return NULL;
  390.  
  391.     central_pos = unzlocal_SearchCentralDir(fin);
  392.     if (central_pos==0)
  393.         err=UNZ_ERRNO;
  394.  
  395.     if (fseek(fin,central_pos,SEEK_SET)!=0)
  396.         err=UNZ_ERRNO;
  397.  
  398.     /* the signature, already checked */
  399.     if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
  400.         err=UNZ_ERRNO;
  401.  
  402.     /* number of this disk */
  403.     if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
  404.         err=UNZ_ERRNO;
  405.  
  406.     /* number of the disk with the start of the central directory */
  407.     if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
  408.         err=UNZ_ERRNO;
  409.  
  410.     /* total number of entries in the central dir on this disk */
  411.     if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
  412.         err=UNZ_ERRNO;
  413.  
  414.     /* total number of entries in the central dir */
  415.     if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
  416.         err=UNZ_ERRNO;
  417.  
  418.     if ((number_entry_CD!=us.gi.number_entry) ||
  419.         (number_disk_with_CD!=0) ||
  420.         (number_disk!=0))
  421.         err=UNZ_BADZIPFILE;
  422.  
  423.     /* size of the central directory */
  424.     if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
  425.         err=UNZ_ERRNO;
  426.  
  427.     /* offset of start of central directory with respect to the
  428.           starting disk number */
  429.     if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
  430.         err=UNZ_ERRNO;
  431.  
  432.     /* zipfile comment length */
  433.     if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
  434.         err=UNZ_ERRNO;
  435.  
  436.     if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
  437.         (err==UNZ_OK))
  438.         err=UNZ_BADZIPFILE;
  439.  
  440.     if (err!=UNZ_OK)
  441.     {
  442.         fclose(fin);
  443.         return NULL;
  444.     }
  445.  
  446.     us.file=fin;
  447.     us.byte_before_the_zipfile = central_pos -
  448.                             (us.offset_central_dir+us.size_central_dir);
  449.     us.central_pos = central_pos;
  450.     us.pfile_in_zip_read = NULL;
  451.  
  452.     us.filename = path;
  453.  
  454.  
  455.     s=(unz_s*)ALLOC(sizeof(unz_s));
  456.     *s=us;
  457.     unzGoToFirstFile((unzFile)s);
  458.     return (unzFile)s;
  459. }
  460.  
  461.  
  462. /*
  463.   Close a ZipFile opened with unzipOpen.
  464.   If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
  465.     these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  466.   return UNZ_OK if there is no problem. */
  467. extern int ZEXPORT unzClose (file)
  468.     unzFile file;
  469. {
  470.     unz_s* s;
  471.     if (file==NULL)
  472.         return UNZ_PARAMERROR;
  473.     s=(unz_s*)file;
  474.  
  475.     if (s->pfile_in_zip_read!=NULL)
  476.         unzCloseCurrentFile(file);
  477.  
  478.     fclose(s->file);
  479.     TRYFREE(s);
  480.     return UNZ_OK;
  481. }
  482.  
  483.  
  484. /*
  485.   Write info about the ZipFile in the *pglobal_info structure.
  486.   No preparation of the structure is needed
  487.   return UNZ_OK if there is no problem. */
  488. extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
  489.     unzFile file;
  490.     unz_global_info *pglobal_info;
  491. {
  492.     unz_s* s;
  493.     if (file==NULL)
  494.         return UNZ_PARAMERROR;
  495.     s=(unz_s*)file;
  496.     *pglobal_info=s->gi;
  497.     return UNZ_OK;
  498. }
  499.  
  500.  
  501. /*
  502.    Translate date/time from Dos format to tm_unz (readable more easilty)
  503. */
  504. local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
  505.     uLong ulDosDate;
  506.     tm_unz* ptm;
  507. {
  508.     uLong uDate;
  509.     uDate = (uLong)(ulDosDate>>16);
  510.     ptm->tm_mday = (uInt)(uDate&0x1f) ;
  511.     ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;
  512.     ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
  513.  
  514.     ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
  515.     ptm->tm_min =  (uInt) ((ulDosDate&0x7E0)/0x20) ;
  516.     ptm->tm_sec =  (uInt) (2*(ulDosDate&0x1f)) ;
  517. }
  518.  
  519. /*
  520.   Get Info about the current file in the zipfile, with internal only info
  521. */
  522. local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
  523.                                                   unz_file_info *pfile_info,
  524.                                                   unz_file_info_internal
  525.                                                   *pfile_info_internal,
  526.                                                   char *szFileName,
  527.                                                   uLong fileNameBufferSize,
  528.                                                   void *extraField,
  529.                                                   uLong extraFieldBufferSize,
  530.                                                   char *szComment,
  531.                                                   uLong commentBufferSize));
  532.  
  533. local int unzlocal_GetCurrentFileInfoInternal (file,
  534.                                               pfile_info,
  535.                                               pfile_info_internal,
  536.                                               szFileName, fileNameBufferSize,
  537.                                               extraField, extraFieldBufferSize,
  538.                                               szComment,  commentBufferSize)
  539.     unzFile file;
  540.     unz_file_info *pfile_info;
  541.     unz_file_info_internal *pfile_info_internal;
  542.     char *szFileName;
  543.     uLong fileNameBufferSize;
  544.     void *extraField;
  545.     uLong extraFieldBufferSize;
  546.     char *szComment;
  547.     uLong commentBufferSize;
  548. {
  549.     unz_s* s;
  550.     unz_file_info file_info;
  551.     unz_file_info_internal file_info_internal;
  552.     int err=UNZ_OK;
  553.     uLong uMagic;
  554.     long lSeek=0;
  555.  
  556.     if (file==NULL)
  557.         return UNZ_PARAMERROR;
  558.     s=(unz_s*)file;
  559.     if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
  560.         err=UNZ_ERRNO;
  561.  
  562.  
  563.     /* we check the magic */
  564.     if (err==UNZ_OK)
  565.         if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  566.             err=UNZ_ERRNO;
  567.         else if (uMagic!=0x02014b50)
  568.             err=UNZ_BADZIPFILE;
  569.  
  570.     if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
  571.         err=UNZ_ERRNO;
  572.  
  573.     if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
  574.         err=UNZ_ERRNO;
  575.  
  576.     if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
  577.         err=UNZ_ERRNO;
  578.  
  579.     if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
  580.         err=UNZ_ERRNO;
  581.  
  582.     if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
  583.         err=UNZ_ERRNO;
  584.  
  585.     unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
  586.  
  587.     if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
  588.         err=UNZ_ERRNO;
  589.  
  590.     if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
  591.         err=UNZ_ERRNO;
  592.  
  593.     if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
  594.         err=UNZ_ERRNO;
  595.  
  596.     if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
  597.         err=UNZ_ERRNO;
  598.  
  599.     if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
  600.         err=UNZ_ERRNO;
  601.  
  602.     if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
  603.         err=UNZ_ERRNO;
  604.  
  605.     if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
  606.         err=UNZ_ERRNO;
  607.  
  608.     if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
  609.         err=UNZ_ERRNO;
  610.  
  611.     if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
  612.         err=UNZ_ERRNO;
  613.  
  614.     if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
  615.         err=UNZ_ERRNO;
  616.  
  617.     lSeek+=file_info.size_filename;
  618.     if ((err==UNZ_OK) && (szFileName!=NULL))
  619.     {
  620.         uLong uSizeRead ;
  621.         if (file_info.size_filename<fileNameBufferSize)
  622.         {
  623.             *(szFileName+file_info.size_filename)='\0';
  624.             uSizeRead = file_info.size_filename;
  625.         }
  626.         else
  627.             uSizeRead = fileNameBufferSize;
  628.  
  629.         if ((file_info.size_filename>0) && (fileNameBufferSize>0))
  630.             if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
  631.                 err=UNZ_ERRNO;
  632.         lSeek -= uSizeRead;
  633.     }
  634.  
  635.  
  636.     if ((err==UNZ_OK) && (extraField!=NULL))
  637.     {
  638.         uLong uSizeRead ;
  639.         if (file_info.size_file_extra<extraFieldBufferSize)
  640.             uSizeRead = file_info.size_file_extra;
  641.         else
  642.             uSizeRead = extraFieldBufferSize;
  643.  
  644.         if (lSeek!=0)
  645.             if (fseek(s->file,lSeek,SEEK_CUR)==0)
  646.                 lSeek=0;
  647.             else
  648.                 err=UNZ_ERRNO;
  649.         if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
  650.             if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1)
  651.                 err=UNZ_ERRNO;
  652.         lSeek += file_info.size_file_extra - uSizeRead;
  653.     }
  654.     else
  655.         lSeek+=file_info.size_file_extra;
  656.  
  657.  
  658.     if ((err==UNZ_OK) && (szComment!=NULL))
  659.     {
  660.         uLong uSizeRead ;
  661.         if (file_info.size_file_comment<commentBufferSize)
  662.         {
  663.             *(szComment+file_info.size_file_comment)='\0';
  664.             uSizeRead = file_info.size_file_comment;
  665.         }
  666.         else
  667.             uSizeRead = commentBufferSize;
  668.  
  669.         if (lSeek!=0)
  670.             if (fseek(s->file,lSeek,SEEK_CUR)==0)
  671.                 lSeek=0;
  672.             else
  673.                 err=UNZ_ERRNO;
  674.         if ((file_info.size_file_comment>0) && (commentBufferSize>0))
  675.             if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1)
  676.                 err=UNZ_ERRNO;
  677.         lSeek+=file_info.size_file_comment - uSizeRead;
  678.     }
  679.     else
  680.         lSeek+=file_info.size_file_comment;
  681.  
  682.     if ((err==UNZ_OK) && (pfile_info!=NULL))
  683.         *pfile_info=file_info;
  684.  
  685.     if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
  686.         *pfile_info_internal=file_info_internal;
  687.  
  688.     return err;
  689. }
  690.  
  691.  
  692.  
  693. /*
  694.   Write info about the ZipFile in the *pglobal_info structure.
  695.   No preparation of the structure is needed
  696.   return UNZ_OK if there is no problem.
  697. */
  698. extern int ZEXPORT unzGetCurrentFileInfo (file,
  699.                                                   pfile_info,
  700.                                                   szFileName, fileNameBufferSize,
  701.                                                   extraField, extraFieldBufferSize,
  702.                                                   szComment,  commentBufferSize)
  703.     unzFile file;
  704.     unz_file_info *pfile_info;
  705.     char *szFileName;
  706.     uLong fileNameBufferSize;
  707.     void *extraField;
  708.     uLong extraFieldBufferSize;
  709.     char *szComment;
  710.     uLong commentBufferSize;
  711. {
  712.     return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
  713.                                                 szFileName,fileNameBufferSize,
  714.                                                 extraField,extraFieldBufferSize,
  715.                                                 szComment,commentBufferSize);
  716. }
  717.  
  718. /*
  719.   Set the current file of the zipfile to the first file.
  720.   return UNZ_OK if there is no problem
  721. */
  722. extern int ZEXPORT unzGoToFirstFile (file)
  723.     unzFile file;
  724. {
  725.     int err=UNZ_OK;
  726.     unz_s* s;
  727.     if (file==NULL)
  728.         return UNZ_PARAMERROR;
  729.     s=(unz_s*)file;
  730.     s->pos_in_central_dir=s->offset_central_dir;
  731.     s->num_file=0;
  732.     err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  733.                                              &s->cur_file_info_internal,
  734.                                              NULL,0,NULL,0,NULL,0);
  735.     s->current_file_ok = (err == UNZ_OK);
  736.     return err;
  737. }
  738.  
  739.  
  740. /*
  741.   Set the current file of the zipfile to the next file.
  742.   return UNZ_OK if there is no problem
  743.   return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  744. */
  745. extern int ZEXPORT unzGoToNextFile (file)
  746.     unzFile file;
  747. {
  748.     unz_s* s;
  749.     int err;
  750.  
  751.     if (file==NULL)
  752.         return UNZ_PARAMERROR;
  753.     s=(unz_s*)file;
  754.     if (!s->current_file_ok)
  755.         return UNZ_END_OF_LIST_OF_FILE;
  756.     if (s->num_file+1==s->gi.number_entry)
  757.         return UNZ_END_OF_LIST_OF_FILE;
  758.  
  759.     s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
  760.             s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
  761.     s->num_file++;
  762.     err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  763.                                                &s->cur_file_info_internal,
  764.                                                NULL,0,NULL,0,NULL,0);
  765.     s->current_file_ok = (err == UNZ_OK);
  766.     return err;
  767. }
  768.  
  769.  
  770. /*
  771.   Try locate the file szFileName in the zipfile.
  772.   For the iCaseSensitivity signification, see unzipStringFileNameCompare
  773.  
  774.   return value :
  775.   UNZ_OK if the file is found. It becomes the current file.
  776.   UNZ_END_OF_LIST_OF_FILE if the file is not found
  777. */
  778. extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
  779.     unzFile file;
  780.     const char *szFileName;
  781.     int iCaseSensitivity;
  782. {
  783.     unz_s* s;
  784.     int err;
  785.  
  786.  
  787.     uLong num_fileSaved;
  788.     uLong pos_in_central_dirSaved;
  789.  
  790.  
  791.     if (file==NULL)
  792.         return UNZ_PARAMERROR;
  793.  
  794.     if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
  795.         return UNZ_PARAMERROR;
  796.  
  797.     s=(unz_s*)file;
  798.     if (!s->current_file_ok)
  799.         return UNZ_END_OF_LIST_OF_FILE;
  800.  
  801.     num_fileSaved = s->num_file;
  802.     pos_in_central_dirSaved = s->pos_in_central_dir;
  803.  
  804.     err = unzGoToFirstFile(file);
  805.  
  806.     while (err == UNZ_OK)
  807.     {
  808.         char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
  809.         unzGetCurrentFileInfo(file,NULL,
  810.                                 szCurrentFileName,sizeof(szCurrentFileName)-1,
  811.                                 NULL,0,NULL,0);
  812.         if (unzStringFileNameCompare(szCurrentFileName,
  813.                                         szFileName,iCaseSensitivity)==0)
  814.             return UNZ_OK;
  815.         err = unzGoToNextFile(file);
  816.     }
  817.  
  818.     s->num_file = num_fileSaved ;
  819.     s->pos_in_central_dir = pos_in_central_dirSaved ;
  820.     return err;
  821. }
  822.  
  823.  
  824. /*
  825.   Read the local header of the current zipfile
  826.   Check the coherency of the local header and info in the end of central
  827.         directory about this file
  828.   store in *piSizeVar the size of extra info in local header
  829.         (filename and size of extra field data)
  830. */
  831. local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
  832.                                                     poffset_local_extrafield,
  833.                                                     psize_local_extrafield)
  834.     unz_s* s;
  835.     uInt* piSizeVar;
  836.     uLong *poffset_local_extrafield;
  837.     uInt  *psize_local_extrafield;
  838. {
  839.     uLong uMagic,uData,uFlags;
  840.     uLong size_filename;
  841.     uLong size_extra_field;
  842.     int err=UNZ_OK;
  843.  
  844.     *piSizeVar = 0;
  845.     *poffset_local_extrafield = 0;
  846.     *psize_local_extrafield = 0;
  847.  
  848.     if (fseek(s->file,s->cur_file_info_internal.offset_curfile +
  849.                                 s->byte_before_the_zipfile,SEEK_SET)!=0)
  850.         return UNZ_ERRNO;
  851.  
  852.  
  853.     if (err==UNZ_OK)
  854.         if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  855.             err=UNZ_ERRNO;
  856.         else if (uMagic!=0x04034b50)
  857.             err=UNZ_BADZIPFILE;
  858.  
  859.     if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  860.         err=UNZ_ERRNO;
  861. /*
  862.     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
  863.         err=UNZ_BADZIPFILE;
  864. */
  865.     if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
  866.         err=UNZ_ERRNO;
  867.  
  868.     if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  869.         err=UNZ_ERRNO;
  870.     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
  871.         err=UNZ_BADZIPFILE;
  872.  
  873.     if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
  874.                          (s->cur_file_info.compression_method!=Z_DEFLATED))
  875.         err=UNZ_BADZIPFILE;
  876.  
  877.     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */
  878.         err=UNZ_ERRNO;
  879.  
  880.     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */
  881.         err=UNZ_ERRNO;
  882.     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
  883.                               ((uFlags & 8)==0))
  884.         err=UNZ_BADZIPFILE;
  885.  
  886.     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */
  887.         err=UNZ_ERRNO;
  888.     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
  889.                               ((uFlags & 8)==0))
  890.         err=UNZ_BADZIPFILE;
  891.  
  892.     if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */
  893.         err=UNZ_ERRNO;
  894.     else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
  895.                               ((uFlags & 8)==0))
  896.         err=UNZ_BADZIPFILE;
  897.  
  898.  
  899.     if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
  900.         err=UNZ_ERRNO;
  901.     else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
  902.         err=UNZ_BADZIPFILE;
  903.  
  904.     *piSizeVar += (uInt)size_filename;
  905.  
  906.     if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
  907.         err=UNZ_ERRNO;
  908.     *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
  909.                                     SIZEZIPLOCALHEADER + size_filename;
  910.     *psize_local_extrafield = (uInt)size_extra_field;
  911.  
  912.     *piSizeVar += (uInt)size_extra_field;
  913.  
  914.     return err;
  915. }
  916.  
  917. /*
  918.   Open for reading data the current file in the zipfile.
  919.   If there is no error and the file is opened, the return value is UNZ_OK.
  920. */
  921. extern int ZEXPORT unzOpenCurrentFile (file)
  922.     unzFile file;
  923. {
  924.     int err=UNZ_OK;
  925.     int Store;
  926.     uInt iSizeVar;
  927.     unz_s* s;
  928.     file_in_zip_read_info_s* pfile_in_zip_read_info;
  929.     uLong offset_local_extrafield;  /* offset of the local extra field */
  930.     uInt  size_local_extrafield;    /* size of the local extra field */
  931.  
  932.     if (file==NULL)
  933.         return UNZ_PARAMERROR;
  934.     s=(unz_s*)file;
  935.     if (!s->current_file_ok)
  936.         return UNZ_PARAMERROR;
  937.  
  938.     if (s->pfile_in_zip_read != NULL)
  939.         unzCloseCurrentFile(file);
  940.  
  941.     if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
  942.                 &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
  943.         return UNZ_BADZIPFILE;
  944.  
  945.     pfile_in_zip_read_info = (file_in_zip_read_info_s*)
  946.                                         ALLOC(sizeof(file_in_zip_read_info_s));
  947.     if (pfile_in_zip_read_info==NULL)
  948.         return UNZ_INTERNALERROR;
  949.  
  950.     pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
  951.     pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
  952.     pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
  953.     pfile_in_zip_read_info->pos_local_extrafield=0;
  954.  
  955.     if (pfile_in_zip_read_info->read_buffer==NULL)
  956.     {
  957.         TRYFREE(pfile_in_zip_read_info);
  958.         return UNZ_INTERNALERROR;
  959.     }
  960.  
  961.     pfile_in_zip_read_info->stream_initialised=0;
  962.  
  963.     if ((s->cur_file_info.compression_method!=0) &&
  964.         (s->cur_file_info.compression_method!=Z_DEFLATED))
  965.         err=UNZ_BADZIPFILE;
  966.     Store = s->cur_file_info.compression_method==0;
  967.  
  968.     pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
  969.     pfile_in_zip_read_info->crc32=0;
  970.     pfile_in_zip_read_info->compression_method =
  971.             s->cur_file_info.compression_method;
  972.     pfile_in_zip_read_info->file=s->file;
  973.     pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
  974.  
  975.     pfile_in_zip_read_info->stream.total_out = 0;
  976.  
  977.     if (!Store)
  978.     {
  979.       pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  980.       pfile_in_zip_read_info->stream.zfree = (free_func)0;
  981.       pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  982.  
  983.       err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
  984.       if (err == Z_OK)
  985.         pfile_in_zip_read_info->stream_initialised=1;
  986.         /* windowBits is passed < 0 to tell that there is no zlib header.
  987.          * Note that in this case inflate *requires* an extra "dummy" byte
  988.          * after the compressed stream in order to complete decompression and
  989.          * return Z_STREAM_END.
  990.          * In unzip, i don't wait absolutely Z_STREAM_END because I known the
  991.          * size of both compressed and uncompressed data
  992.          */
  993.     }
  994.     pfile_in_zip_read_info->rest_read_compressed =
  995.             s->cur_file_info.compressed_size ;
  996.     pfile_in_zip_read_info->rest_read_uncompressed =
  997.             s->cur_file_info.uncompressed_size ;
  998.  
  999.  
  1000.     pfile_in_zip_read_info->pos_in_zipfile =
  1001.             s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
  1002.               iSizeVar;
  1003.  
  1004.     pfile_in_zip_read_info->stream.avail_in = (uInt)0;
  1005.  
  1006.  
  1007.     s->pfile_in_zip_read = pfile_in_zip_read_info;
  1008.     return UNZ_OK;
  1009. }
  1010.  
  1011.  
  1012. /*
  1013.   Read bytes from the current file.
  1014.   buf contain buffer where data must be copied
  1015.   len the size of buf.
  1016.  
  1017.   return the number of byte copied if somes bytes are copied
  1018.   return 0 if the end of file was reached
  1019.   return <0 with error code if there is an error
  1020.     (UNZ_ERRNO for IO error, or zLib error for uncompress error)
  1021. */
  1022. extern int ZEXPORT unzReadCurrentFile  (file, buf, len)
  1023.     unzFile file;
  1024.     voidp buf;
  1025.     unsigned len;
  1026. {
  1027.     int err=UNZ_OK;
  1028.     uInt iRead = 0;
  1029.     unz_s* s;
  1030.     file_in_zip_read_info_s* pfile_in_zip_read_info;
  1031.     if (file==NULL)
  1032.         return UNZ_PARAMERROR;
  1033.     s=(unz_s*)file;
  1034.     pfile_in_zip_read_info=s->pfile_in_zip_read;
  1035.  
  1036.     if (pfile_in_zip_read_info==NULL)
  1037.         return UNZ_PARAMERROR;
  1038.  
  1039.  
  1040.     if ((pfile_in_zip_read_info->read_buffer == NULL))
  1041.         return UNZ_END_OF_LIST_OF_FILE;
  1042.     if (len==0)
  1043.         return 0;
  1044.  
  1045.     pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
  1046.  
  1047.     pfile_in_zip_read_info->stream.avail_out = (uInt)len;
  1048.  
  1049.     if (len>pfile_in_zip_read_info->rest_read_uncompressed)
  1050.         pfile_in_zip_read_info->stream.avail_out =
  1051.           (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
  1052.  
  1053.     while (pfile_in_zip_read_info->stream.avail_out>0)
  1054.     {
  1055.         if ((pfile_in_zip_read_info->stream.avail_in==0) &&
  1056.             (pfile_in_zip_read_info->rest_read_compressed>0))
  1057.         {
  1058.             uInt uReadThis = UNZ_BUFSIZE;
  1059.             if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
  1060.                 uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
  1061.             if (uReadThis == 0)
  1062.                 return UNZ_EOF;
  1063.             if (fseek(pfile_in_zip_read_info->file,
  1064.                       pfile_in_zip_read_info->pos_in_zipfile +
  1065.                          pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0)
  1066.                 return UNZ_ERRNO;
  1067.             if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1,
  1068.                          pfile_in_zip_read_info->file)!=1)
  1069.                 return UNZ_ERRNO;
  1070.             pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
  1071.  
  1072.             pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
  1073.  
  1074.             pfile_in_zip_read_info->stream.next_in =
  1075.                 (Bytef*)pfile_in_zip_read_info->read_buffer;
  1076.             pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
  1077.         }
  1078.  
  1079.         if (pfile_in_zip_read_info->compression_method==0)
  1080.         {
  1081.             uInt uDoCopy,i ;
  1082.             if (pfile_in_zip_read_info->stream.avail_out <
  1083.                             pfile_in_zip_read_info->stream.avail_in)
  1084.                 uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
  1085.             else
  1086.                 uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
  1087.  
  1088.             for (i=0;i<uDoCopy;i++)
  1089.                 *(pfile_in_zip_read_info->stream.next_out+i) =
  1090.                         *(pfile_in_zip_read_info->stream.next_in+i);
  1091.  
  1092.             pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
  1093.                                 pfile_in_zip_read_info->stream.next_out,
  1094.                                 uDoCopy);
  1095.             pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
  1096.             pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
  1097.             pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
  1098.             pfile_in_zip_read_info->stream.next_out += uDoCopy;
  1099.             pfile_in_zip_read_info->stream.next_in += uDoCopy;
  1100.             pfile_in_zip_read_info->stream.total_out += uDoCopy;
  1101.             iRead += uDoCopy;
  1102.         }
  1103.         else
  1104.         {
  1105.             uLong uTotalOutBefore,uTotalOutAfter;
  1106.             const Bytef *bufBefore;
  1107.             uLong uOutThis;
  1108.             int flush=Z_SYNC_FLUSH;
  1109.  
  1110.             uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
  1111.             bufBefore = pfile_in_zip_read_info->stream.next_out;
  1112.  
  1113.             /*
  1114.             if ((pfile_in_zip_read_info->rest_read_uncompressed ==
  1115.                      pfile_in_zip_read_info->stream.avail_out) &&
  1116.                 (pfile_in_zip_read_info->rest_read_compressed == 0))
  1117.                 flush = Z_FINISH;
  1118.             */
  1119.             err=inflate(&pfile_in_zip_read_info->stream,flush);
  1120.  
  1121.             uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
  1122.             uOutThis = uTotalOutAfter-uTotalOutBefore;
  1123.  
  1124.             pfile_in_zip_read_info->crc32 =
  1125.                 crc32(pfile_in_zip_read_info->crc32,bufBefore,
  1126.                         (uInt)(uOutThis));
  1127.  
  1128.             pfile_in_zip_read_info->rest_read_uncompressed -=
  1129.                 uOutThis;
  1130.  
  1131.             iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
  1132.  
  1133.             if (err==Z_STREAM_END)
  1134.                 return (iRead==0) ? UNZ_EOF : iRead;
  1135.             if (err!=Z_OK)
  1136.                 break;
  1137.         }
  1138.     }
  1139.  
  1140.     if (err==Z_OK)
  1141.         return iRead;
  1142.     return err;
  1143. }
  1144.  
  1145.  
  1146. /*
  1147.   Give the current position in uncompressed data
  1148. */
  1149. extern z_off_t ZEXPORT unztell (file)
  1150.     unzFile file;
  1151. {
  1152.     unz_s* s;
  1153.     file_in_zip_read_info_s* pfile_in_zip_read_info;
  1154.     if (file==NULL)
  1155.         return UNZ_PARAMERROR;
  1156.     s=(unz_s*)file;
  1157.     pfile_in_zip_read_info=s->pfile_in_zip_read;
  1158.  
  1159.     if (pfile_in_zip_read_info==NULL)
  1160.         return UNZ_PARAMERROR;
  1161.  
  1162.     return (z_off_t)pfile_in_zip_read_info->stream.total_out;
  1163. }
  1164.  
  1165.  
  1166. /*
  1167.   return 1 if the end of file was reached, 0 elsewhere
  1168. */
  1169. extern int ZEXPORT unzeof (file)
  1170.     unzFile file;
  1171. {
  1172.     unz_s* s;
  1173.     file_in_zip_read_info_s* pfile_in_zip_read_info;
  1174.     if (file==NULL)
  1175.         return UNZ_PARAMERROR;
  1176.     s=(unz_s*)file;
  1177.     pfile_in_zip_read_info=s->pfile_in_zip_read;
  1178.  
  1179.     if (pfile_in_zip_read_info==NULL)
  1180.         return UNZ_PARAMERROR;
  1181.  
  1182.     if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  1183.         return 1;
  1184.     else
  1185.         return 0;
  1186. }
  1187.  
  1188.  
  1189.  
  1190. /*
  1191.   Read extra field from the current file (opened by unzOpenCurrentFile)
  1192.   This is the local-header version of the extra field (sometimes, there is
  1193.     more info in the local-header version than in the central-header)
  1194.  
  1195.   if buf==NULL, it return the size of the local extra field that can be read
  1196.  
  1197.   if buf!=NULL, len is the size of the buffer, the extra header is copied in
  1198.     buf.
  1199.   the return value is the number of bytes copied in buf, or (if <0)
  1200.     the error code
  1201. */
  1202. extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
  1203.     unzFile file;
  1204.     voidp buf;
  1205.     unsigned len;
  1206. {
  1207.     unz_s* s;
  1208.     file_in_zip_read_info_s* pfile_in_zip_read_info;
  1209.     uInt read_now;
  1210.     uLong size_to_read;
  1211.  
  1212.     if (file==NULL)
  1213.         return UNZ_PARAMERROR;
  1214.     s=(unz_s*)file;
  1215.     pfile_in_zip_read_info=s->pfile_in_zip_read;
  1216.  
  1217.     if (pfile_in_zip_read_info==NULL)
  1218.         return UNZ_PARAMERROR;
  1219.  
  1220.     size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
  1221.                 pfile_in_zip_read_info->pos_local_extrafield);
  1222.  
  1223.     if (buf==NULL)
  1224.         return (int)size_to_read;
  1225.  
  1226.     if (len>size_to_read)
  1227.         read_now = (uInt)size_to_read;
  1228.     else
  1229.         read_now = (uInt)len ;
  1230.  
  1231.     if (read_now==0)
  1232.         return 0;
  1233.  
  1234.     if (fseek(pfile_in_zip_read_info->file,
  1235.               pfile_in_zip_read_info->offset_local_extrafield +
  1236.               pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
  1237.         return UNZ_ERRNO;
  1238.  
  1239.     if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
  1240.         return UNZ_ERRNO;
  1241.  
  1242.     return (int)read_now;
  1243. }
  1244.  
  1245. /*
  1246.   Close the file in zip opened with unzipOpenCurrentFile
  1247.   Return UNZ_CRCERROR if all the file was read but the CRC is not good
  1248. */
  1249. extern int ZEXPORT unzCloseCurrentFile (file)
  1250.     unzFile file;
  1251. {
  1252.     int err=UNZ_OK;
  1253.  
  1254.     unz_s* s;
  1255.     file_in_zip_read_info_s* pfile_in_zip_read_info;
  1256.     if (file==NULL)
  1257.         return UNZ_PARAMERROR;
  1258.     s=(unz_s*)file;
  1259.     pfile_in_zip_read_info=s->pfile_in_zip_read;
  1260.  
  1261.     if (pfile_in_zip_read_info==NULL)
  1262.         return UNZ_PARAMERROR;
  1263.  
  1264.  
  1265.     if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  1266.     {
  1267.         if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
  1268.             err=UNZ_CRCERROR;
  1269.     }
  1270.  
  1271.  
  1272.     TRYFREE(pfile_in_zip_read_info->read_buffer);
  1273.     pfile_in_zip_read_info->read_buffer = NULL;
  1274.     if (pfile_in_zip_read_info->stream_initialised)
  1275.         inflateEnd(&pfile_in_zip_read_info->stream);
  1276.  
  1277.     pfile_in_zip_read_info->stream_initialised = 0;
  1278.     TRYFREE(pfile_in_zip_read_info);
  1279.  
  1280.     s->pfile_in_zip_read=NULL;
  1281.  
  1282.     return err;
  1283. }
  1284.  
  1285.  
  1286. /*
  1287.   Get the global comment string of the ZipFile, in the szComment buffer.
  1288.   uSizeBuf is the size of the szComment buffer.
  1289.   return the number of byte copied or an error code <0
  1290. */
  1291. extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
  1292.     unzFile file;
  1293.     char *szComment;
  1294.     uLong uSizeBuf;
  1295. {
  1296.     int err=UNZ_OK;
  1297.     unz_s* s;
  1298.     uLong uReadThis ;
  1299.     if (file==NULL)
  1300.         return UNZ_PARAMERROR;
  1301.     s=(unz_s*)file;
  1302.  
  1303.     uReadThis = uSizeBuf;
  1304.     if (uReadThis>s->gi.size_comment)
  1305.         uReadThis = s->gi.size_comment;
  1306.  
  1307.     if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0)
  1308.         return UNZ_ERRNO;
  1309.  
  1310.     if (uReadThis>0)
  1311.     {
  1312.       *szComment='\0';
  1313.       if (fread(szComment,(uInt)uReadThis,1,s->file)!=1)
  1314.         return UNZ_ERRNO;
  1315.     }
  1316.  
  1317.     if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
  1318.         *(szComment+s->gi.size_comment)='\0';
  1319.     return (int)uReadThis;
  1320. }
  1321.